package com.lvmama.dashboard.core.ssh;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;

import com.lvmama.java.rhino.spark.core.ICallback;
import com.lvmama.rhino.utils.JedisClusterUtils;

public class SaveShellLogRunner implements Runnable, Serializable {

	private static final long serialVersionUID = 2049859136045347346L;
	
	private int execId;
	private BufferedReader reader;
	private BufferedWriter writer;
	private ICallback callback;
	
	public SaveShellLogRunner(int execId, BufferedReader reader, BufferedWriter writer, ICallback callback) {
		this.execId = execId;
		this.reader = reader;
		this.writer = writer;
		this.callback = callback;
	}
	
	@Override
	public void run() {
		try {
			String line = reader.readLine();
			while(line != null) {
				JedisClusterUtils.getInstance().lpush("SHELL_EXEC_LOG_" + execId, line);
				writer.write(line);
				line = reader.readLine();
			}
			if(callback != null) {
				callback.call(null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
